<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button icon="el-icon-refresh" type="primary" size="mini" @click="Refresh"></el-button>
        <el-button size="small" type="primary" @click="dialogFormVisible = true">导出</el-button>
      </div>
      <div class="table_searchs flex">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="用户名" value="2"></el-option>
              <el-option label="sn" value="3"></el-option>
              <el-option label="批次" value="4"></el-option>
              <!-- <el-option label="状态" value="5"></el-option> -->
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- 分类管理table -->
    <el-table stripe :data="brandTable" style="width: 100%" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" prop="name" label="操作账号" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[1].istrue" prop="operation" label="推送类型" min-width="140" align="center"></el-table-column>
      <el-table-column v-if="colData[2].istrue" prop="type" label="操作类型" min-width="140" align="center"></el-table-column>
      <el-table-column v-if="colData[3].istrue" prop="create_time" label="操作时间" min-width="160" align="center">
        <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
      </el-table-column>
      <el-table-column v-if="colData[4].istrue" prop="sn" label="SN" min-width="150" align="center"></el-table-column>
      <el-table-column v-if="colData[5].istrue" prop="batch" label="批次" min-width="150" align="center"></el-table-column>
      <el-table-column v-if="colData[6].istrue" prop="package_name" label="包名" min-width="200" align="center"></el-table-column>
      <el-table-column v-if="colData[7].istrue" prop="code" label="状态码" min-width="150" align="center"></el-table-column>
      <el-table-column v-if="colData[8].istrue" prop="status" label="推送状态" min-width="150" align="center">
        <template slot-scope="scope">{{ scope.row.status == 'error' ? '失败' : '成功' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[9].istrue" prop="message_id" label="message_id" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[10].istrue" prop="message" label="提示消息" min-width="300" align="center"></el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" :page-sizes="[10, 50, 100, 200]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </div>

    <el-dialog title="导出" :visible.sync="dialogFormVisible" width="30%" :close-on-click-modal="false" :close-on-press-escape="false" @close="redactFormclose">
      <el-form :model="addForm" ref="ruleForm" :rules="rules">
        <!-- <el-form-item label="归属:" label-width="120px" prop="aId">
          <el-select v-model="addForm.aId" placeholder="没有选中任何项">
            <el-option v-for="item in userInfo" :key="item.id" :label="item.username" :value="item.id"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="时间段:" label-width="100px" >
          <el-date-picker v-model="timeForm" type="daterange" :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format='yyyy-MM-dd'></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini" round>取 消</el-button>
        <el-button type="primary" @click="getexportPushLog('ruleForm')" size="mini" round>确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryPushLog } from "@/api/control";
import { getSelectUserId } from "@/api/dashboard.js";
import { exportPushLog } from "@/api/infos.js";
export default {
  components: { dynamicTable },
  data() {
    return {
      checkboxVal: ["操作账号", "推送类型", "操作类型", "操作时间", "SN", "批次", "包名", "状态码", "推送状态", "message_id", "提示消息"],
      key: 1,
      colData: [], //动态控制table
      brandTable: [],
      select: "", //搜索条件
      searchTable: "",
      total: 0, //总数量
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), type: 1, name: undefined, sn: undefined, batch: undefined, code: undefined },
      userInfo: [],
      dialogFormVisible: false,
      addForm: { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip") },
      rules: { aId: [{ required: true, trigger: "blur", message: "请选择归属" }], timeForm: [{ required: true, trigger: "blur", message: "请选择时间段" }] },
      timeForm: '',
      pickerMinDate: '',
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.pickerMinDate = minDate.getTime()
          if (maxDate) {
            this.pickerMinDate = ''
          }
        },
        disabledDate: (time) => {
          if (this.pickerMinDate !== '') {
            const day30 = (30 - 1) * 24 * 3600 * 1000
            let maxTime = this.pickerMinDate + day30
            if (maxTime > new Date()) {
              maxTime = new Date()
            }
            return time.getTime() > maxTime
          }
          return time.getTime() > Date.now() + 8.64e7
        }
      }
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getqueryPushLog()
    this.getSelectUserId()
  },
  methods: {
    //导出日志
    getexportPushLog(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.startTime = this.timeForm[0]
          addForm.endTime = this.timeForm[1]
          exportPushLog(addForm).then(({ data }) => {
            window.open('https://' + data.url)
          }).catch(err => {
            err.code == 400 ? this.$message.error(err.msg) : ''
          })
        } else {
          return false
        }
      })
    },
    //获取归属
    getSelectUserId() {
      getSelectUserId({ userId: localStorage.getItem("ID") }).then(res => {
        this.userInfo = res.data
      }).catch(err => {
        this.userInfo = []
      })
    },
    getqueryPushLog() {
      queryPushLog(this.listQuery).then(res => {
        this.brandTable = res.data
        this.total = res.total
      }).catch(err => {
        this.brandTable = []
        this.total = 0
      })
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), type: 1, name: undefined, sn: undefined, batch: undefined, code: undefined }
      this.getqueryPushLog()
    },
    //搜索,1:用户名,2:sn,3:批次,4:状态
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '2':
          this.listQuery.type = 2
          this.listQuery.username = this.searchTable
          this.getqueryPushLog()
          break;
        case '3':
          this.listQuery.type = 3
          this.listQuery.sn = this.searchTable
          this.getqueryPushLog()
          break;
        case '4':
          this.listQuery.type = 4
          this.listQuery.batch = this.searchTable
          this.getqueryPushLog()
          break;
        case '5':
          this.listQuery.type = 5
          this.listQuery.code = this.searchTable
          this.getqueryPushLog()
          break;
      }
    },
    //分页
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryPushLog();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryPushLog();
    },
    redactFormclose() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
        this.timeForm = ''
      })
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>